package com.qianxx.component.activity.image.stack_view;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianxx.component.R;
import com.qianxx.component.activity.base.BaseActivity;
import com.qianxx.viewlibrary.image.stackview.StackViewVertical;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StackViewActivity extends BaseActivity {
    @BindView(R.id.stack_view_vertical)
    StackViewVertical stack_view_vertical;

    List<Integer> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stack_view);
        ButterKnife.bind(this);

        initData();
        stack_view_vertical.setAdapter(new StackViewAdapter(this, list));
    }

    private void initData() {
        list.add(R.drawable.p1);
        list.add(R.drawable.p2);
        list.add(R.drawable.p3);
        list.add(R.drawable.p4);
        list.add(R.drawable.p5);
        list.add(R.drawable.p6);
        list.add(R.drawable.p7);
        list.add(R.drawable.p8);
    }

    class StackViewAdapter extends BaseAdapter {

        private Context mContext;
        private List<Integer> mPhotos;

        public StackViewAdapter(Context context, List<Integer> photos) {
            mContext = context;
            mPhotos = photos;
        }

        @Override
        public int getCount() {
            if (mPhotos == null) {
                return 0;
            } else {
                return mPhotos.size();
            }
        }

        @Override
        public Integer getItem(int position) {
            return mPhotos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View view, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (view == null) {
                LayoutInflater vi = (LayoutInflater) mContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.item_stack_view, null, false);
                viewHolder = new ViewHolder();
                viewHolder.mPhotoView = (ImageView) view
                        .findViewById(R.id.slider_item_image);
                viewHolder.mCounter = (TextView) view
                        .findViewById(R.id.slider_item_count);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.mPhotoView.setImageResource(mPhotos.get(position));
            viewHolder.mCounter.setText((position + 1) + "/" + getCount());
            return view;
        }

        class ViewHolder {
            private ImageView mPhotoView;
            private TextView mCounter;
        }
    }

    @Override
    public String setTitle() {
        return "StackView";
    }
}